package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.ItemOrder;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.hdw.ItemOrderMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品订单业务
 * 
 * @Date:Created in 2018/7/10 15:55
 * @Author:
 * @Modified By: zjn 2018/7/10 15:55
 */ 
@Service
public class ItemOrderService extends AbstractService {
    @Autowired
    ItemOrderMapper itemOrderMapper;

    public ItemOrder find(String id) {
        return itemOrderMapper.find(id);
    }
    /**
    * 分页查询
    * 
    * @Param: [orders]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/10  15:55
    */ 
    public Page findPage(ItemOrder orders) {
        Page page = orders.buildPage();
        page.setTotalItems(itemOrderMapper.findPageCount(orders));
        orders.setBeginIndex(page.getOffset());
        page.setResult(setItemProperties(itemOrderMapper.findPageResult(orders)));
        return page;
    }
    /**
    * 统计价钱
    * 
    * @Param: [createTime]
    * @Return: java.util.Map<java.lang.String,java.lang.Object>
    * @Author By: zjn 2018/7/10  15:55
    */ 
    public Map<String,Object> findToDayItemPrice(Date createTime) {
        // 根据 已付款 订单状态统计1天的总价钱
        return itemOrderMapper.findToDayItemPrice(ItemOrder.OrderStatus.PAY.getValue(), createTime);
    }
    /**
    * 查询
    * 
    * @Param: [orders]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  16:00
    */ 
    public ExtResult updateOrderStatus(ItemOrder orders) {
        ItemOrder itemOrder = itemOrderMapper.find(orders.getId());
        if (itemOrder == null) {
            return ExtResult.failResult("订单编号不存在");
        }
        if (itemOrder.getOrderStatus() != ItemOrder.OrderStatus.PAY.getValue()) {
            return ExtResult.failResult("订单" + itemOrder.getOrderStatusName());
        }
        if (itemOrderMapper.updateOrderStatus(orders.getId(), ItemOrder.OrderStatus.DELIVERED.getValue(), ItemOrder.OrderStatus.PAY.getValue(), orders.getExpressName()) == 0) {
            return ExtResult.failResult("服务器内部出错");
        }
        return ExtResult.successResult();
    }
}

